package com.jabo.douban.demo.presentation.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import com.jabo.douban.demo.R;
import com.jabo.douban.demo.databinding.HomeFragmentBinding;
import com.jabo.douban.demo.presentation.view.adapter.HomeFragmentPagerAdapter;
import com.jabo.douban.demo.presentation.viewmodel.HomeViewModel;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private HomeViewModel mViewModel;
    private HomeFragmentBinding mHomeFragmentBinding;
    private HomeFragmentPagerAdapter mPagerAdapter;
    private List<Fragment> mFragments;

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mHomeFragmentBinding = DataBindingUtil.inflate(inflater, R.layout.home_fragment, container, false);
        return mHomeFragmentBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
        mFragments = new ArrayList<>();
        mFragments.add(DynamicFragment.newInstance());
        mFragments.add(RecommendFragment.newInstance());
        //帶指示器的Fragment滑動組件
        mPagerAdapter = new HomeFragmentPagerAdapter(getChildFragmentManager(), mFragments);
        mHomeFragmentBinding.viewPager.setAdapter(mPagerAdapter);
        mHomeFragmentBinding.tabLayout.setupWithViewPager(mHomeFragmentBinding.viewPager);

        mHomeFragmentBinding.include.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateSearchFragment();
            }
        });
    }

    public void navigateSearchFragment() {
        Navigation.findNavController(getView()).navigate(R.id.searchFragment, null);
    }

}
